﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VueIServices;
using VueModel;

namespace VueProject.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;
    
        public UserController(IUserService userService)
        {
            _userService = userService;   
        }

        [Route("api/User/AddUser"),HttpPost]
        public Response AddUser(UserModel model)
        {
            if (!string.IsNullOrEmpty(model.RoleIdString))
            {
                model.RoleId = model.RoleIdString.Split(',').Select<string, int>(s => Convert.ToInt32(s)).ToArray();
            }
            var addUser =  _userService.AddUser(model);
            return new Response() { Data = addUser };
        }

        [Route("DelUser"), HttpPost]
        public bool DelUser(int userId)
        {
            return _userService.DelUser(userId);

        }

        [Route("api/User/GetUserList"), HttpGet]
        public List<UserModel> GetUserList()
        {
            return _userService.GetUserList();
        }

        [Route("GetUsers"), HttpGet]
        public ResultModel<List<UserModel>> GetUsers()
        {
            return new ResultModel<List<UserModel>>
            {
                Data = _userService.GetUserList(),
                Total = _userService.GetUserList().Count
            };
        }

        [Route("ModefyUser"), HttpPost]
        public Response ModefyUser(UserModel model)
        {
            if (!string.IsNullOrEmpty(model.RoleIdString))
            {
                model.RoleId = model.RoleIdString.Split(',').Select<string, int>(s => Convert.ToInt32(s)).ToArray();
            }

            var modefyUser =  _userService.ModefyUser(model);
            return new Response() { Data = modefyUser };
        }
    }
}
